package com.neu.carbon.scm.service.impl;

import java.util.List;
import com.neu.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neu.carbon.scm.domain.ScmSaleReturnDetail;
import com.neu.carbon.scm.mapper.ScmSaleReturnMapper;
import com.neu.carbon.scm.domain.ScmSaleReturn;
import com.neu.carbon.scm.service.IScmSaleReturnService;

/**
 * 销售退货Service业务层处理
 * 
 * @author neuedu
 * @date 2025-03-17
 */
@Service
public class ScmSaleReturnServiceImpl implements IScmSaleReturnService 
{
    @Autowired
    private ScmSaleReturnMapper scmSaleReturnMapper;

    /**
     * 查询销售退货
     * 
     * @param id 销售退货ID
     * @return 销售退货
     */
    @Override
    public ScmSaleReturn selectScmSaleReturnById(Long id)
    {
        return scmSaleReturnMapper.selectScmSaleReturnById(id);
    }

    /**
     * 查询销售退货列表
     * 
     * @param scmSaleReturn 销售退货
     * @return 销售退货
     */
    @Override
    public List<ScmSaleReturn> selectScmSaleReturnList(ScmSaleReturn scmSaleReturn)
    {
        return scmSaleReturnMapper.selectScmSaleReturnList(scmSaleReturn);
    }

    /**
     * 新增销售退货
     * 
     * @param scmSaleReturn 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScmSaleReturn(ScmSaleReturn scmSaleReturn)
    {
        scmSaleReturn.setCreateTime(DateUtils.getNowDate());
        int rows = scmSaleReturnMapper.insertScmSaleReturn(scmSaleReturn);
        insertScmSaleReturnDetail(scmSaleReturn);
        return rows;
    }

    /**
     * 修改销售退货
     * 
     * @param scmSaleReturn 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScmSaleReturn(ScmSaleReturn scmSaleReturn)
    {
        scmSaleReturnMapper.deleteScmSaleReturnDetailByReturnId(scmSaleReturn.getId());
        insertScmSaleReturnDetail(scmSaleReturn);
        return scmSaleReturnMapper.updateScmSaleReturn(scmSaleReturn);
    }

    /**
     * 批量删除销售退货
     * 
     * @param ids 需要删除的销售退货ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmSaleReturnByIds(Long[] ids)
    {
        scmSaleReturnMapper.deleteScmSaleReturnDetailByReturnIds(ids);
        return scmSaleReturnMapper.deleteScmSaleReturnByIds(ids);
    }

    /**
     * 删除销售退货信息
     * 
     * @param id 销售退货ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmSaleReturnById(Long id)
    {
        scmSaleReturnMapper.deleteScmSaleReturnDetailByReturnId(id);
        return scmSaleReturnMapper.deleteScmSaleReturnById(id);
    }

    /**
     * 新增退货明细信息
     * 
     * @param scmSaleReturn 销售退货对象
     */
    public void insertScmSaleReturnDetail(ScmSaleReturn scmSaleReturn)
    {
        List<ScmSaleReturnDetail> scmSaleReturnDetailList = scmSaleReturn.getScmSaleReturnDetailList();
        Long id = scmSaleReturn.getId();
        if (StringUtils.isNotNull(scmSaleReturnDetailList))
        {
            List<ScmSaleReturnDetail> list = new ArrayList<ScmSaleReturnDetail>();
            for (ScmSaleReturnDetail scmSaleReturnDetail : scmSaleReturnDetailList)
            {
                scmSaleReturnDetail.setReturnId(id);
                list.add(scmSaleReturnDetail);
            }
            if (list.size() > 0)
            {
                scmSaleReturnMapper.batchScmSaleReturnDetail(list);
            }
        }
    }
}
